﻿namespace IPMS.BLL
{
    using IPMS.DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Client
    {
        private string _Address;
        private string _ChineseName;
        private string _Client;
        private string _ContactPerson;
        private string _ContactPerson2;
        private string _EmailAddress;
        private string _EmailAddress2;
        private string _Fax;
        private string _Fax2;
        private string _JobTitle;
        private string _MobilePhone;
        private string _MobilePhone2;
        private string _Name;
        private string _Phone;
        private string _Phone2;
        private string _WebSite;
        private bool _IsDelete;

        public Client(string Client)
        {
            this._Client = Client;
        }

        public Client(string Client, string name, string chineseName, string address, string contactPerson, string jobTitle, string phone, string fax)
        {
            this._Client = Client;
            this._Name = name;
            this._ChineseName = chineseName;
            this._Address = address;
            this._ContactPerson = contactPerson;
            this._JobTitle = jobTitle;
            this._Phone = phone;
            this._Fax = fax;
        }

        public int GetClientDetails()
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            DAL.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Client", SqlDbType.NVarChar, 0xff, ParameterDirection.Input, this._Client.ToString());
            DAL.AddParamToSQLCmd(sqlCmd, "@Name", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ChineseName", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Address", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ContactPerson", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@JobTitle", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Phone", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Phone2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@MobilePhone", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@MobilePhone2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@WebSite", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@EmailAddress", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@EmailAddress2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ContactPerson2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Fax2", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Output, null);
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, "GetClientDetailsSp");
            DAL.ExecuteScalarCmd(sqlCmd);
            int resultValue = (int) sqlCmd.Parameters["@ReturnValue"].Value;
            this.Client_Name = IPDataFormat.ValidString(sqlCmd.Parameters["@Name"].Value.ToString());
            this.Client_ChineseName = IPDataFormat.ValidString(sqlCmd.Parameters["@ChineseName"].Value.ToString());
            this.Client_Address = IPDataFormat.ValidString(sqlCmd.Parameters["@Address"].Value.ToString());
            this.Client_ContactPerson = IPDataFormat.ValidString(sqlCmd.Parameters["@ContactPerson"].Value.ToString());
            this.Client_JobTitle = IPDataFormat.ValidString(sqlCmd.Parameters["@JobTitle"].Value.ToString());
            this.Client_Phone = IPDataFormat.ValidString(sqlCmd.Parameters["@Phone"].Value.ToString());
            this.Client_Fax = IPDataFormat.ValidString(sqlCmd.Parameters["@Fax"].Value.ToString());
            this.Client_Phone2 = IPDataFormat.ValidString(sqlCmd.Parameters["@Phone2"].Value.ToString());
            this.Client_MobilePhone = IPDataFormat.ValidString(sqlCmd.Parameters["@MobilePhone"].Value.ToString());
            this.Client_MobilePhone2 = IPDataFormat.ValidString(sqlCmd.Parameters["@MobilePhone2"].Value.ToString());
            this.Client_WebSite = IPDataFormat.ValidString(sqlCmd.Parameters["@WebSite"].Value.ToString());
            this.Client_EmailAddress = IPDataFormat.ValidString(sqlCmd.Parameters["@EmailAddress"].Value.ToString());
            this.Client_EmailAddress2 = IPDataFormat.ValidString(sqlCmd.Parameters["@EmailAddress2"].Value.ToString());
            this.Client_ContactPerson2 = IPDataFormat.ValidString(sqlCmd.Parameters["@ContactPerson2"].Value.ToString());
            this.Client_Fax2 = IPDataFormat.ValidString(sqlCmd.Parameters["@Fax2"].Value.ToString());

            if (sqlCmd.Parameters["@IsDelete"] != null && sqlCmd.Parameters["@IsDelete"].Value.ToString().Trim().Length > 0)
            {
                this.Client_IsDelete = Convert.ToBoolean(sqlCmd.Parameters["@IsDelete"].Value);
            }
            else
            {
                this.Client_IsDelete = false;
            }

            return resultValue;
        }

        public bool Update(bool IsDelete, string UpdateBy, string UpdateIP, string client)
        {
            bool isSuccess = false;
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                DataAccessLayer DAL = new DataAccessLayer();

                string sqlString = "UPDATE [Client] SET IsDelete = @IsDelete,UpdateBy = @UpdateBy,UpdateDate = GETDATE(), UpdateIP = @UpdateIP WHERE (([Client] = @Client)) ";

                DAL.SetCommandType(sqlCommand, CommandType.Text, sqlString);
                DAL.AddParamToSQLCmd(sqlCommand, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Input, IsDelete);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, UpdateBy);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateIP", SqlDbType.NVarChar, 50, ParameterDirection.Input, UpdateIP);
                DAL.AddParamToSQLCmd(sqlCommand, "@Client", SqlDbType.NVarChar, 60, ParameterDirection.Input, client);
                DAL.ExecuteScalarCmd(sqlCommand);

                isSuccess = true;
            }
            catch { }

            return isSuccess;
        }

        public string Client_Address
        {
            get
            {
                return this._Address;
            }
            set
            {
                this._Address = value;
            }
        }

        public string Client_ChineseName
        {
            get
            {
                return this._ChineseName;
            }
            set
            {
                this._ChineseName = value;
            }
        }

        public string Client_ContactPerson
        {
            get
            {
                return this._ContactPerson;
            }
            set
            {
                this._ContactPerson = value;
            }
        }

        public string Client_ContactPerson2
        {
            get
            {
                return this._ContactPerson2;
            }
            set
            {
                this._ContactPerson2 = value;
            }
        }

        public string Client_EmailAddress
        {
            get
            {
                return this._EmailAddress;
            }
            set
            {
                this._EmailAddress = value;
            }
        }

        public string Client_EmailAddress2
        {
            get
            {
                return this._EmailAddress2;
            }
            set
            {
                this._EmailAddress2 = value;
            }
        }

        public string Client_Fax
        {
            get
            {
                return this._Fax;
            }
            set
            {
                this._Fax = value;
            }
        }

        public string Client_Fax2
        {
            get
            {
                return this._Fax2;
            }
            set
            {
                this._Fax2 = value;
            }
        }

        public string Client_JobTitle
        {
            get
            {
                return this._JobTitle;
            }
            set
            {
                this._JobTitle = value;
            }
        }

        public string Client_MobilePhone
        {
            get
            {
                return this._MobilePhone;
            }
            set
            {
                this._MobilePhone = value;
            }
        }

        public string Client_MobilePhone2
        {
            get
            {
                return this._MobilePhone2;
            }
            set
            {
                this._MobilePhone2 = value;
            }
        }

        public string Client_Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public string Client_Phone
        {
            get
            {
                return this._Phone;
            }
            set
            {
                this._Phone = value;
            }
        }

        public string Client_Phone2
        {
            get
            {
                return this._Phone2;
            }
            set
            {
                this._Phone2 = value;
            }
        }

        public string Client_WebSite
        {
            get
            {
                return this._WebSite;
            }
            set
            {
                this._WebSite = value;
            }
        }

        public bool Client_IsDelete
        {
            get
            {
                return this._IsDelete;
            }
            set
            {
                this._IsDelete = value;
            }
        }
    }
}

